package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.GlideRoundTransform;
import com.egg.ylt.pojo.SwitchBabyEntity;
import com.yonyou.framework.library.adapter.rv.CommonAdapter;
import com.yonyou.framework.library.adapter.rv.ViewHolder;
import com.yonyou.framework.library.eventbus.EventCenter;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by lijiateng on 2019/1/21.
 */

public class ADA_SwitchBaby extends CommonAdapter<SwitchBabyEntity.ListBean> {

    public SwitchBabyEntity.ListBean mPreItem;

    public ADA_SwitchBaby(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_switch_baby;
    }

    @Override
    protected void convert(ViewHolder holder, final SwitchBabyEntity.ListBean switchBabyEntity, int position) {

//        if (mPreItem == null && switchBabyEntity.isChecked()) {
//            mPreItem = switchBabyEntity;
//        }

        holder.setVisible(R.id.iv_select, switchBabyEntity.isChecked());



        if(switchBabyEntity.getGender().equals("0")){
            Glide.with(mContext)
                    .load(switchBabyEntity.getImageUrl())
                    .transform(new GlideRoundTransform(mContext, 999))
                    .transition(new DrawableTransitionOptions().crossFade())
                    .placeholder(R.mipmap.addbaby_img_boy)
                    .error(R.mipmap.addbaby_img_boy)
                    .into((ImageView) holder.getView(R.id.iv_logo));
        }else {
            Glide.with(mContext)
                    .load(switchBabyEntity.getImageUrl())
                    .transform(new GlideRoundTransform(mContext, 999))
                    .transition(new DrawableTransitionOptions().crossFade())
                    .placeholder(R.mipmap.addbaby_img_girl)
                    .error(R.mipmap.addbaby_img_girl)
                    .into((ImageView) holder.getView(R.id.iv_logo));
        }

        holder.setText(R.id.tv_name, switchBabyEntity.getName());
        holder.setText(R.id.tv_age, switchBabyEntity.getBirthday());
        holder.setText(R.id.tv_record_count, switchBabyEntity.getCount());

        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//
//                    if (mPreItem == switchBabyEntity) {
//
//                        return;
//                    }
//
//                    if (mPreItem == null) {
//                        switchBabyEntity.setChecked(true);
//                        mPreItem = switchBabyEntity;
//                        notifyDataSetChanged();
//                        return;
//                    }
//
//                    switchBabyEntity.setChecked(true);
//                    mPreItem.setChecked(false);
//                    mPreItem = switchBabyEntity;

                EventBus.getDefault().post(new EventCenter(Constants.SWITCH_BABY_EVENT, switchBabyEntity.getId()));


            }
        });

    }

    public String getCheckedBabyId() {
        if (mPreItem != null) {
            return mPreItem.getId();
        }
        return "";
    }

}
